/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * assigndoctorservice.java
 *
 * Created on Jul 14, 2011, 10:33:52 PM
 */
package manager;

import generalCode.Connectionsql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author kaka
 */
public class assigndoctorservice extends javax.swing.JFrame {
    //create ArratList
    private ArrayList<AssignemployeeserviceDTO> ListAssignservice = new ArrayList<AssignemployeeserviceDTO> ();
    Vector<String> Columname;
    {
        Columname = new Vector<String>();
        Columname.add("Id");
        Columname.add("Name");
        Columname.add("Department");
        Columname.add("Service");
        Columname.add("Datetime");
                
    }
    /* Creates new form assigndoctorservice */
    public assigndoctorservice() throws SQLException {
        initComponents();
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        initData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbserveservice = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tffullname = new javax.swing.JTextField();
        tfdepartment = new javax.swing.JTextField();
        tfservice = new javax.swing.JTextField();
        btdone = new javax.swing.JButton();
        btexit = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        tfDatetime = new javax.swing.JTextField();
        btcancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Assign Employees serve service", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 255, 0))); // NOI18N

        tbserveservice.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Id", "Full name", "Department", "serve service"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbserveservice.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbserveserviceMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbserveservice);

        jLabel1.setText("Full name:");

        jLabel2.setText("Department:");

        jLabel3.setText("Service:");

        tffullname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tffullnameActionPerformed(evt);
            }
        });

        btdone.setText("Done");
        btdone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdoneActionPerformed(evt);
            }
        });

        btexit.setText("Exit");

        jLabel4.setText("Datetime:");

        btcancel.setText("Cancel");
        btcancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btcancelMouseClicked(evt);
            }
        });
        btcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(8, 8, 8))
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfDatetime, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(tfservice, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tfdepartment, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(tffullname, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btcancel)
                            .addComponent(btexit)
                            .addComponent(btdone)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tffullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btdone)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(tfdepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfservice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfDatetime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btexit)
                        .addGap(18, 18, 18)
                        .addComponent(btcancel)
                        .addGap(11, 11, 11)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tffullnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tffullnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tffullnameActionPerformed

    private void btdoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btdoneActionPerformed
        try {
            Assign();
        } catch (SQLException ex) {
            Logger.getLogger(assigndoctorservice.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btdoneActionPerformed

    private void tbserveserviceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbserveserviceMouseClicked
        ShowData();
    }//GEN-LAST:event_tbserveserviceMouseClicked

    private void btcancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btcancelMouseClicked

    }//GEN-LAST:event_btcancelMouseClicked

    private void btcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btcancelActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btcancel;
    private javax.swing.JButton btdone;
    private javax.swing.JButton btexit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbserveservice;
    private javax.swing.JTextField tfDatetime;
    private javax.swing.JTextField tfdepartment;
    private javax.swing.JTextField tffullname;
    private javax.swing.JTextField tfservice;
    // End of variables declaration//GEN-END:variables

    private void initData() throws SQLException {
         Connectionsql db = Connectionsql.newInstance();
         String sql = "SELECT Employee.FullName, Employee.Deparment, Employee.[Level], Appointment.Date, Service.ServiceName FROM Appointment INNER JOIN Appointment_Service ON Appointment.AppointmentID = Appointment_Service.AppointmentID INNER JOIN Employee ON Appointment.EmployeeID = Employee.EmployeeID INNER JOIN Service ON Appointment_Service.ServiceID = Service.ServiceID";
         ResultSet rs = db.query(sql);
         while(rs.next()){
             ListAssignservice.add(new AssignemployeeserviceDTO(rs.getString("fullName"),rs.getString("Deparment"),
                     rs.getString("Servicename"),rs.getString("date") ));
                      
         }
         db.closeConn();
         Vector<Vector<String>>Data = new Vector<Vector<String>>();
         for(int i =0;i<ListAssignservice.size();i++){
         Vector<String> v = new Vector<String>();
         v.add(ListAssignservice.get(i).getName());
         v.add(ListAssignservice.get(i).getDepartment());
         v.add(ListAssignservice.get(i).getService());
         v.add(ListAssignservice.get(i).getDatetime()+"");
         Data.add(v);
         }
         tbserveservice.setModel(new DefaultTableModel(Data,Columname));
    }

    private void Assign() throws SQLException {
       if(validateinput()){
       Connectionsql db = Connectionsql.newInstance();
       String sql="insert into Assignservice("+getText(tffullname)
       +","+getText(tfDatetime)
       +","+getText(tfservice)
       +","+getText(tfDatetime)
       +",)";
       System.out.println(sql);
       if(db.update(sql))
           JOptionPane.showMessageDialog(this, "Assign success");
       initData();
       } 
    }

    private void ShowData() {
       int SelectRow =-1;
       SelectRow = tbserveservice.getSelectedRow();
       if(SelectRow !=1){
       tffullname.setText(ListAssignservice.get(SelectRow).getName());
       tfdepartment.setText(ListAssignservice.get(SelectRow).getDepartment());
       tfservice.setText(ListAssignservice.get(SelectRow).getService());
       tfDatetime.setText(ListAssignservice.get(SelectRow).getDatetime()+"");
       }
    }

    private boolean validateinput() {
        if(tffullname.getText().trim().isEmpty()&&tfdepartment.getText().trim().isEmpty()&&tfservice.getText().trim().isEmpty()&&tfDatetime.getText().trim().isEmpty()){
            JOptionPane.showMessageDialog(this, "input all");
            return false;
            }
        return true;
    }
    private String getText(JTextField txt){
    return txt.getText().trim();
    }
     
}          
